#!/bin/bash
#
# The script processes exported from WebCT exportedcourse.csv file fed as command line argument or via stdin.
# It generates directory tree for enrolled students. 
#
# Usage: lofstud.sh [<exportedcourse.csv>]
# where <exportedcourse.csv> is a file with the roll of students exported from gaView (blackboard).

# SVN specific:
#---------------
# $Author: simonbaev $
# $LastChangedDate: 2012-01-03 15:03:16 -0500 (Tue, 03 Jan 2012) $
# $Revision: 63 $
#

# Initialization
fname="-"
[ -n "$cis1000path" ] && eval cd "$cis1000path" 2> /dev/null
if [ $# -ge 1 ]; then
	if [ -f "$1" ]; then
		fname="$1"
	else
		echo "File '$1' doesn't exist." 1>&2
		echo "Usage: $0 [<exportedcourse.csv>]" 1>&2
		echo "Note: stdin is used if no paramenter is passed." 1>&2
		exit 1
	fi
fi
index=1
rm -rf Students
# Name of the course designer
dname=sbaev
# Main processing
tail -n +2 "$fname" | while read line; do
	last=$(echo "$line" | cut -f1 -d"," | tr -d '"');
	first=$(echo "$line" | cut -f2 -d"," | tr -d '"');
	userid=$(echo "$line" | cut -f3 -d",");
	email=$(echo "$userid@radar.gsw.edu");
	flname=$(printf "%s %s" "$first" "$last" | tr '[A-Z]' '[a-z]' | tr '-' '_');
	if [ "$userid" != "$dname" ]; then
		printf "%02d: %s\n" $((index++)) "$flname";
		mkdir -p "Students/$flname"
		printf "%-10s%s %s\n%-10s%s\n" \
				"Name:" "$first" "$last" "E-Mail:" "$email" \
				> "Students/$flname/info.txt"
	fi
done
